package com.hcrazy.manchester.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.SearchForm;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.SecondaryRequire;
import com.hcrazy.manchester.app.services.SecondaryRequireService;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/func")
public class SecondaryRequireController {
	
	@Autowired
	private SecondaryRequireService secondaryRequireService;
	
	@RequestMapping(value="/viewSecondaryRequire",method=RequestMethod.GET)
	public void viewSecondaryRequire(@RequestParam long infoId,Model model){
		
		SecondaryRequire secondary = null;
		try {
			secondary = secondaryRequireService.getInfoById(infoId);
			model.addAttribute("secondary", secondary);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addSecondaryRequire",method=RequestMethod.GET)
	public void addSecondaryRequire(Model model){
		//初始化新增对象
		SecondaryRequire secondary = new SecondaryRequire();
		model.addAttribute("secondary", secondary);
	}
	
	/**
	 * 编辑信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editSecondaryRequire",method=RequestMethod.GET)
	public String editSecondaryRequire(@RequestParam int infoId,Model model){
		try {
			//初始化对象内容
			addSecondaryRequire(model);
			//根据id获取信息内容
			SecondaryRequire secondary = secondaryRequireService.getInfoById(infoId);
			//更新返回对象
			model.addAttribute("secondary",secondary);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/func/addSecondaryRequire";
	}
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/secondaryRequire",method=RequestMethod.GET)
	public void secondaryRequire(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllSecondarys(1,"",null);
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", new SearchForm());
	}
	
	/**
	 * 提交信息内容
	 * @param secondary
	 * @return
	 */
	@RequestMapping(value="/secondaryRequire",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateSort(@ModelAttribute("form") SecondaryRequire secondary,
    		Model model){
		try {
			secondaryRequireService.addOrUpdateSecondaryRequire(secondary);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/func/secondaryRequire";
    }
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_secondary_require",method=RequestMethod.POST)
	public String searchSecondaryRequire(@ModelAttribute("form") SearchForm form,Model model){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			try {
				secondaryRequireService.delSecondary(form.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllSecondarys(form.getPageNum(),form.getSearch(),form.getUsername());
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/func/secondaryRequire";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllSecondarys(int pageNum,String search,String username){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return secondaryRequireService.getSecondaryInfos(model, search,username);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	

}
